YUI().use('autocomplete', 'autocomplete-highlighters', 'event', 'tabview', 'gallery-widget-io', 'node', 'io-base', 'json-parse', function (Y) {
    Y.one('#query').plug(Y.Plugin.AutoComplete, {
    resultHighlighter: 'phraseMatch',
    source: 'uniwikiac.php?query={query}'
  });
 
      function successHandler(id, o){
        var node = Y.one('#description');
        var loadingNode = Y.one('#loading');
        loadingNode.setContent("");
        Y.log("Success handler called; handler will parse the retrieved XML and insert into DOM.", "info", "example");
        Y.log("Success handler is complete.", "info", "example");
        node.setContent("");
        //Catch JSON parse failures.
        try {
          var oData = Y.JSON.parse(o.responseText);
          paras = oData.parse.text['*'].match(/<p(.)*<\/p>/g);
          for (var i = 0; i < paras.length; i++) {
              node.append(paras[i]);
          }
        }
        catch (e) {
          Y.log("JSON Parse failed");
          return;
        }
      }

      //Provide a function that can help debug failed
      //requests:
      function failureHandler(id, o){
        var loadingNode = Y.one('#loading');
        loadingNode.setContent("");
        Y.log("Failure handler called; http status: " + o.status, "info", "example");
        //div.set("innerHTML", o.status + " " + o.statusText);
      }
      function getDescription(){
        var node = Y.one('#loading');
        node.setContent('<img src="loading.gif" height="30" width="30" />');
        //Create a querystring from the input value:
        var uni_name = document.getElementById('uni_name').innerHTML;
        var queryString = encodeURI('?action=parse&page=' + uni_name + '&format=json');

        //The location of our server-side proxy:
        var entryPoint = 'proxy.cgi';

        //Compile the full URI for the request:
        var sUrl = entryPoint + queryString;

        var request = Y.io(sUrl, {
          method:"GET",
          on:
          {
            success:successHandler,
            failure:failureHandler
          }
        });

        //prevent refreshing page by user pressing the enter key.
        var prevent = function(e) {
          e.preventDefault();
        }
      }
    if(Y.one('#map_canvas')) {
        Y.on("domready", getDescription, "#description");
        Y.on("domready", initialize);
        var uni_name = document.getElementById('uni_name').innerHTML;
        var feeds = {
            Ranking: 'ranking.php?query='+uni_name,
            Photos: 'flickr_proxy.cgi?query='+uni_name,
            Tweets: 'twitter.php?query='+uni_name,
            Books: 'books_proxy.cgi?&q='+uni_name
        },
     
        TabIO = function(config) {
            TabIO.superclass.constructor.apply(this, arguments); 
        };
     
        Y.extend(TabIO, Y.Plugin.WidgetIO, {
            initializer: function() {
                var tab = this.get('host');
                tab.on('selectedChange', this.afterSelectedChange);
            },
     
            afterSelectedChange: function(e) { // this === tab
                if (e.newVal) { // tab has been selected
                    this.io.refresh();
                }
            },
     
            setContent: function(content) {
                var tab = this.get('host');
                tab.set('content', content);
            },
     
            _toggleLoadingClass: function(add) {
                this.get('host').get('panelNode')
                    .toggleClass(this.get('host').getClassName('loading'), add);
            }
     
        }, {
            NAME: 'tabIO',
            NS: 'io'
        });
     
        var tabview = new Y.TabView();
     
        Y.each(feeds, function(src, label) {
            tabview.add({
                label: label,
                plugins: [{
                    fn: TabIO, 
                    cfg: {
                        uri: src
                    }
                }]
            });
        });
        tabview.render('#tabs');
    }
 
});

function initialize() {
    var latitude = document.getElementById("latitude").innerHTML;
    var longitude = document.getElementById("longitude").innerHTML;
    var uni_name = document.getElementById('uni_name').innerHTML;
    var latlng = new google.maps.LatLng(latitude, longitude);
    var contentString = document.getElementById("tabs");
    var myOptions = {
      zoom: 12,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);
    var marker = new google.maps.Marker({
        position: latlng, 
        map: map,
        animation: google.maps.Animation.DROP,
        title:uni_name
    });   
    var infowindow = new google.maps.InfoWindow({
        content: contentString
    });
    google.maps.event.addListener(marker, 'click', function() {
      if(document.getElementById("tabs")) {
        document.getElementById("tabs").style.display = 'block';
      }
      infowindow.open(map,marker);
    });
}

